/*
 * Copyright (C) 2009 Internal Innovation Technology(IIT)
 * 
 */
package com.iitbd.hxcalc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URL;
import javax.help.CSH;
import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.*;
import org.apache.log4j.Logger;

/**
 *
 * @author Prasun Kanti Howlader <prasun@iitbd.com>
 */
public class HxcalcUI extends JFrame {

    static final long serialVersionUID = 0L;
    public static Insets menuInset = null;
    private JLabel currentFilterName = null;
    private static Logger logger = Logger.getLogger(HxcalcUI.class.getName());    
    public InputOutputPanel inputwindow = new InputOutputPanel();

    public HxcalcUI() throws Exception {

        // super("hccalc");

        try {
            LookAndFeel laf = UIManager.getLookAndFeel();
            if (laf.getClass().getName().equals("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel")) {
                menuInset = new Insets(1, -10, 1, 1);
            } else {
                menuInset = new Insets(1, 1, 1, 1);
            }

            initialize();


        } catch (Exception e) {
            //logger.error(MiscUtils.stackTrace2String(e));
            throw e;
        }
    }

    public void initialize() throws Exception {

        Container cp = getContentPane();

        cp.add(inputwindow);
        setJMenuBar(createMenuBar());

        updateStatusPane();

        Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        setPreferredSize(new Dimension((int) d.getWidth() - 50, (int) d.getHeight() - 100));

        initializeActionMap();     


        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
            }
        });

    }

    @SuppressWarnings("serial")
    private void initializeActionMap() {
        ActionMap am = getRootPane().getActionMap();
        InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        Object obj1 = new Object();
        Object obj2 = new Object();

        KeyStroke closeFindStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);

        Action closeFindAction = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
            }
        };

        im.put(closeFindStroke, obj2);
        am.put(obj2, closeFindAction);
    }

    private JPanel getStatusPane() {
        JLabel username = new JLabel("Logged in as: ");
        JPanel ret = new JPanel();
        ret.setLayout(new BorderLayout());
        return ret;
    }

    public void updateStatusPane() {
    }

    private JPanel getOutPane() {
        JPanel ret = new JPanel();
        ret.setLayout(new GridLayout(1, 1, 5, 5));


        return ret;
    }

    @SuppressWarnings("serial")
    private JPanel getInputPane() {
        JPanel ret = new JPanel();
        ret.setLayout(new BorderLayout());

        return ret;
    }

    /**
     * Create a menu
     *
     * @return Return and Object of JmenuBar
     *
     */
    private JMenuBar createMenuBar() {

        JMenuBar mbar = new JMenuBar();
        JMenuItem mItem;
        JMenu menu;

        //File menu begin
        menu = new JMenu(Resources.getString("File"));
        menu.setMnemonic(KeyEvent.VK_F);



        mItem = new JMenuItem(Resources.getString("Open input file"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_I);
        mItem.setActionCommand("Open input file");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Open output file"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_O);
        mItem.setActionCommand("Open output file");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Close output file"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_Z);
        mItem.setActionCommand("Close output file");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Exit"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_X);
        mItem.setActionCommand("Exit");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mbar.add(menu);
        //File menu End

        //Edit menu begin
        menu = new JMenu(Resources.getString("Edit"));
        menu.setMnemonic(KeyEvent.VK_F);



        mItem = new JMenuItem(Resources.getString("Copy all"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_I);
        mItem.setActionCommand("Copy all");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Cut"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_O);
        mItem.setActionCommand("Cut");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Copy"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_Z);
        mItem.setActionCommand("Copy");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Paste"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_X);
        mItem.setActionCommand("Paste");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mbar.add(menu);
        //End Edit menu

        //Begin Config menu
        menu = new JMenu(Resources.getString("Config"));
        menu.setMnemonic(KeyEvent.VK_F);


        mItem = new JMenuItem(Resources.getString("Default"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_I);
        mItem.setActionCommand("Default");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Save"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_O);
        mItem.setActionCommand("Save");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Load"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_Z);
        mItem.setActionCommand("Load");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Properties"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_X);
        mItem.setActionCommand("Properties");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mbar.add(menu);
        //End config menu
        //Begining of Help menu

        menu = new JMenu(Resources.getString("Help"));
        menu.setMnemonic(KeyEvent.VK_F);


        mItem = new JMenuItem(Resources.getString("Help"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_I);
        HelpSet hs = getHelpSet("Humicalhelp/Sample.hs");
        HelpBroker hb = hs.createHelpBroker();

        // 2. assign help to components
        CSH.setHelpIDString(mItem, "top");

        // 3. handle events
        mItem.addActionListener(new CSH.DisplayHelpFromSource(hb));
        //mItem.setActionCommand("Help");
        //mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Support"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_O);
        mItem.setActionCommand("Support");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Registration"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_Z);
        mItem.setActionCommand("Registration");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Update"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_X);
        mItem.setActionCommand("Update");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mItem = new JMenuItem(Resources.getString("Info about"));
        mItem.setMargin(menuInset);
        mItem.setMnemonic(KeyEvent.VK_X);
        mItem.setActionCommand("Info about");
        mItem.addActionListener(new MenuItemHandler());
        menu.add(mItem);

        mbar.add(menu);
        //Ending of Help menu
        return mbar;
    }

    class MenuItemHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();

            if (cmd.equals("Open input file")) {
                showInputDialog();

                return;
            }
            if (cmd.equals("Open output file")) {
                showOutputDialog();

                return;
            }
            if (cmd.equals("Close output file")) {
                HxCalc.instance.getUtils().setFileName(null);
                return;
            }

            if (cmd.equals("Exit")) {
                System.exit(0);//Exit From Application
                return;
            }


            if (cmd.equals("Default")) {
                try {
                    HxCalc.getConfig().save();
                } catch (Exception ie) {
                }

                return;
            }
            if (cmd.equals("Save")) {


                return;
            }
            if (cmd.equals("Load")) {


                return;
            }
            if (cmd.equals("Info about")) {
                showAboutDialog();
                return;
            }

            if (cmd.equals("Properties")) {
                showConfigDialog();

                return;
            }
            if (cmd.equals("Registration")) {
                try {
                    //Set your page url in this string. For eg, I m using URL for Google Search engine
                    String url = AppDefaults.REGISTRATION_PAGE;
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
                } catch (java.io.IOException io) {
                    System.out.println(io.getMessage());
                }
            }
            if (cmd.equals("Update")) {
                try {
                    //Set your page url in this string. For eg, I m using URL for Google Search engine
                    String url = AppDefaults.UPDATE_PAGE;
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
                } catch (java.io.IOException io) {
                    System.out.println(io.getMessage());
                }
            }

        }
    }

    public HelpSet getHelpSet(String helpsetfile) {
        HelpSet hs = null;
        ClassLoader cl = this.getClass().getClassLoader();
        try {
            URL hsURL = HelpSet.findHelpSet(cl, helpsetfile);
            hs = new HelpSet(null, hsURL);
        } catch (Exception ee) {
            System.out.println("HelpSet: " + ee.getMessage());
            System.out.println("HelpSet: " + helpsetfile + " not found");
        }
        return hs;
    }

    private void showConfigDialog() {
        //ConfigurationDialog d = new ConfigurationDialog(this);
        ConfigDialog d = new ConfigDialog(this, Resources.getString("appTitle"));
        d.pack();
        d.setLocationRelativeTo(this);
        d.setVisible(true);
    }

    /**
     * Show about Dialog
     *
     * @return
     */
    private void showAboutDialog() {
        //ConfigurationDialog d = new ConfigurationDialog(this);
        InfoAbout d = new InfoAbout(this, Resources.getString("appTitle"));
        d.pack();
        d.setLocationRelativeTo(this);
        d.setVisible(true);
    }

    private void showInputDialog() {
        FileInputDialog d = new FileInputDialog(this, Resources.getString("appTitle"));
        d.pack();
        d.setLocationRelativeTo(this);
        d.setVisible(true);

    }

    private void showOutputDialog() {
        FileOutputDialog d = new FileOutputDialog(this, Resources.getString("appTitle"));
        d.pack();
        d.setLocationRelativeTo(this);
        d.setVisible(true);
    }
}
